package com.storm.lib.listener;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;

import org.apache.log4j.Logger;

import com.storm.lib.base.BasePo;
import com.storm.lib.template.MemoryTemplate;
import com.storm.lib.util.BeanUtil;
import com.storm.lib.util.DBUtil;

public class CacheEvtLstn implements CacheEventListener{
	protected final Logger logger = Logger.getLogger(getClass());
	@Override
	public Object clone(){

		return this;
	}

	@Override
	public void dispose() {

	}

	@Override
	public void notifyElementEvicted(Ehcache arg0, Element arg1) {
		MemoryTemplate memoryTemplate = (MemoryTemplate) BeanUtil.getBean("memoryTemplate");
		BasePo basePo = (BasePo) arg1.getValue();
		logger.debug("量满淘汰"+basePo.getClass().getSimpleName()+basePo.getId());
		if (DBUtil.isDBSyncClass(basePo)) {
			memoryTemplate.syncToDB(basePo);
		}
	}

	@Override
	public void notifyElementExpired(Ehcache arg0, Element arg1) {
		MemoryTemplate memoryTemplate = (MemoryTemplate) BeanUtil.getBean("memoryTemplate");
		BasePo basePo = (BasePo) arg1.getValue();
		logger.debug("过期淘汰"+basePo.getClass().getSimpleName()+basePo.getId());
		if (DBUtil.isDBSyncClass(basePo)) {
			memoryTemplate.syncToDB(basePo);
		}
	}

	@Override
	public void notifyElementPut(Ehcache arg0, Element arg1)
			throws CacheException {


	}

	@Override
	public void notifyElementRemoved(Ehcache arg0, Element arg1)
			throws CacheException {

	}

	@Override
	public void notifyElementUpdated(Ehcache arg0, Element arg1)
			throws CacheException {


	}

	@Override
	public void notifyRemoveAll(Ehcache cache) {

	}

}
